package com.zh.system.controller;

import com.zh.common.annotation.Operator;
import com.zh.system.domain.dto.AddDicTypeDto;
import com.zh.system.domain.dto.AddDicValueDto;
import com.zh.system.domain.dto.UpdateDicTypeDto;
import com.zh.system.domain.dto.UpdateDicValDto;
import com.zh.system.domain.entity.SysDicType;
import com.zh.system.domain.vo.SysDicValTreeVo;
import com.zh.system.service.SysDicService;
import com.zh.system.service.SysDicValueService;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Valid
@Slf4j
@RestController
@RequestMapping("/dictionary")
@RequiredArgsConstructor
@Api(tags = "系统服务-字典接口")
@Operator
public class DicController extends BaseController {
    private final SysDicService sysDicService;
    private final SysDicValueService sysDicValueService;

    @ApiOperation("获取所有字典type-value")
    @GetMapping("/all")
    public ApiResult<List<SysDicValTreeVo>> getAllDic() {
        return buildSuccessPackage(sysDicService.getDicTreeByType());
    }

    @ApiOperation("获取所有根字典类型,用于筛选字典")
    @GetMapping("/rootType")
    public ApiResult<List<SysDicType>> getRootDicType() {
        return buildSuccessPackage(sysDicService.selectRootTypeList());
    }

    @ApiOperation("获取该类型字典")
    @GetMapping("/{type}")
    public ApiResult<List<SysDicValTreeVo>> getDicByType(@PathVariable @NotEmpty String type) {
        return buildSuccessPackage(sysDicService.getDicTreeByType(type));
    }

    @ApiOperation("添加字典类型")
    @PostMapping("/type/add")
    public ApiResult<Boolean> addDicType(@RequestBody AddDicTypeDto addDicTypeDto) {
        addDicTypeDto.setCreateOperator(operator);
        addDicTypeDto.setUpdateOperator(operator);
        return buildSuccessPackage(sysDicService.addDicType(addDicTypeDto));
    }

    @ApiOperation("修改字典类型")
    @PutMapping("/type/update")
    public ApiResult<Boolean> updateDicType(@RequestBody UpdateDicTypeDto updateDicTypeDto) {
        updateDicTypeDto.setUpdateOperator(operator);
        return buildSuccessPackage(sysDicService.updateDicType(updateDicTypeDto));
    }

    @ApiOperation("删除字典键,其下有子字典键无法删除,删除后其下字典值一并删除")
    @DeleteMapping("/type/delete")
    public ApiResult<String> deleteDicType(@RequestParam @NotEmpty Long id) {
        return buildMsgPackage(sysDicService.deleteDicType(id));
    }

    @ApiOperation("添加字典值")
    @PostMapping("/value/add")
    public ApiResult<Boolean> addDicValue(@RequestBody AddDicValueDto addDicTypeDto) {
        addDicTypeDto.setCreateOperator(operator);
        addDicTypeDto.setUpdateOperator(operator);
        return buildSuccessPackage(sysDicValueService.addDicValue(addDicTypeDto));
    }

    @ApiOperation("修改字典值")
    @PutMapping("/value/update")
    public ApiResult<String> updateDicValue(@RequestBody UpdateDicValDto updateDicValDto) {
        updateDicValDto.setUpdateOperator(operator);
        return buildMsgPackage(sysDicValueService.updateDicValue(updateDicValDto));
    }

    @ApiOperation("删除字典值")
    @DeleteMapping("/value/delete")
    public ApiResult<Boolean> deleteDicValue(@RequestParam @NotEmpty Long id) {
        return buildSuccessPackage(sysDicValueService.deleteDicValue(id));
    }
}
